package com.zhehekeji.opsys.api.service.validate;

import org.apache.cxf.common.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.zhehekeji.opsys.api.code.UserRegisterResultCode;
import com.zhehekeji.opsys.api.entity.TUser;
import com.zhehekeji.opsys.api.resp.CheckRegisterResp;
import com.zhehekeji.opsys.api.service.validate.args.UserRegisterArgs;

/**
 * @ClassName CheckRegisterValidate
 * @Description TODO
 * @author chenweiliu chenwei.liu@163.com
 * @date 2015-7-3   下午4:20:43
 */
@Service
public class CheckRegisterValidate extends ValidateService<ValidateRets<TUser>, UserRegisterArgs>{

	private final static Logger logger = LoggerFactory.getLogger(CheckRegisterValidate.class);
	
	@Override
	public ValidateRets<TUser> validate(UserRegisterArgs validateArgs)
			throws Exception {
		String tmpAccount = validateArgs.getAccount();
		Long tmpCpCode = validateArgs.getCpCode();
		CheckRegisterResp resp = (CheckRegisterResp)validateArgs.getResp();
		logger.info("账号:"+tmpAccount+",CpCode:"+tmpCpCode);
		ValidateRets<TUser> validateRets = new ValidateRets<TUser>();
		if (StringUtils.isEmpty(tmpAccount)){
			logger.error("检查失败，账号为空！");
			resp.setResultCode(UserRegisterResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("检查失败，账号非法！");
			validateRets.setSuccess(false);
			return validateRets;
		}
//		if (tmpCpCode == null || tmpCpCode <= 0){
//			logger.error("账号"+tmpAccount+"，cpCode为空！");
//			resp.setResultCode(UserRegisterResultCode.PARAMETER_IS_NOT_LEGAL);
//			resp.setResultDesc("注册失败，cpCode非法！");
//			validateRets.setSuccess(false);
//			return validateRets;
//		}
//		TCompany tmpTCompany = entityDao.get(TCompany.class, "o.code=?1", new Object[]{tmpCpCode});
//		if (tmpTCompany == null){
//			logger.error("注册失败，企业不存在，不存在此cpCode："+tmpCpCode);
//			resp.setResultCode(UserRegisterResultCode.PARAMETER_IS_NOT_LEGAL);
//			resp.setResultDesc("注册失败，企业不存在，cpCode非法！");
//			validateRets.setSuccess(false);
//			return validateRets;
//		}
//		Integer tmpStatus = tmpTCompany.getStatus();
//		if (tmpStatus == 0){
//			logger.error("注册失败，企业CpCode"+tmpCpCode+"已经停用！");
//			resp.setResultCode(UserRegisterResultCode.COMPANY_HAS_BEEN_STOPPED);
//			resp.setResultDesc("注册失败，企业已经停用！");
//			validateRets.setSuccess(true);
//			return validateRets;
//		}
		TUser tmpTUser = entityDao.get(TUser.class, "o.account=?1", new Object[]{tmpAccount});
		if (tmpTUser != null){
			logger.info("检查成功，用户已注册！");
			resp.setRegister(true);
			resp.setResultDesc("检查成功，用户已注册！");
			validateRets.setEntity(tmpTUser);
			validateRets.setSuccess(true);
		}else{
			logger.info("检查成功，用户未注册！");
			resp.setRegister(false);
			resp.setResultDesc("检查成功，用户未注册！");
			validateRets.setSuccess(true);
		}
		return validateRets;
		
	}

}
